package cn.com.checne.controller.superManager;

import cn.com.checne.constants.Constants;
import cn.com.checne.controller.BaseController;
import cn.com.checne.dao.CultureCategoryDao;
import cn.com.checne.dao.CultureInfoDao;
import cn.com.checne.dao.LogInfoDao;
import cn.com.checne.model.domain.CultureCategory;
import cn.com.checne.model.domain.CultureInfo;
import cn.com.checne.model.dto.CultureQuery;
import cn.com.checne.util.PageBean;
import cn.com.checne.util.StringTools;
import cn.com.checne.util.TimeUtils;
import cn.com.checne.util.WebUtils;
import cn.com.checne.util.images.ImageContentUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/super/culture")
public class CultureInfoManagerController extends BaseController {
	protected static final Logger logger = Logger.getLogger(CultureInfoManagerController.class);

	@Resource
	private CultureCategoryDao cateDao;
	@Resource
	private CultureInfoDao cultureDao;
	@Resource
	private LogInfoDao logDao;
	
	//企业文化--首页
	@RequestMapping(value="/index.do")
	public String index(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "super/frame/frame_culture";
	}
	
	//企业文化--左侧导航
	@RequestMapping(value="/left.do")
	public String left(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "super/culture/left";
	}
	
	//企业文化--列举所有的栏目
	@RequestMapping(value="/category-list.do")
	public ModelAndView categoryList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int languageId = (String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE) != null ? Integer.parseInt((String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE)) : 1 ;
		List<CultureCategory> cateList = cateDao.getAll(languageId);
		modelMap.put("cateList", cateList);
		modelMap.put("languageId", languageId);
		mav.setViewName("super/culture/category-list");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//企业文化--栏目新增、修改页面
	@RequestMapping(value="/category-show.do")
	public ModelAndView categoryShow(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId", 0);
		int languageId = (String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE) != null ? Integer.parseInt((String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE)) : 1 ;

		CultureCategory cate = new CultureCategory();
		cate.setLanguageId(languageId);
		String opt = "create" ;
		if(cateId > 0){
			cate = cateDao.getCultureCategory(cateId);
			opt = "update" ;
		}
		modelMap.put("cate", cate);
		modelMap.put("opt", opt);
		mav.setViewName("super/culture/category-show");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//企业文化--栏目新增
	@RequestMapping(value="/category-create.do")
	public ModelAndView categoryCreate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		CultureCategory cate = new CultureCategory();
		cate.setLanguageId(WebUtils.getRequestParameterAsInt(request, "languageId",1));
		cate.setShowType(WebUtils.getRequestParameterAsInt(request, "showType",0));
		cate.setName(WebUtils.getRequestParameterAsString(request, "name"));
		cate.setDesc(WebUtils.getRequestParameterAsString(request, "desc"));
		cate.setOrder(WebUtils.getRequestParameterAsInt(request, "order",0));
		if(cateDao.create(cate)){
			message = "企业文化栏目创建成功!" ;
		}else{
			message = "企业文化栏目创建失败!" ;
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/culture/category-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//企业文化--栏目修改
	@RequestMapping(value="/category-update.do")
	public ModelAndView categoryUpdate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		CultureCategory cate = new CultureCategory();
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId", 0);
		cate = cateDao.getCultureCategory(cateId);
		cate.setLanguageId(WebUtils.getRequestParameterAsInt(request, "languageId",1));
		cate.setShowType(WebUtils.getRequestParameterAsInt(request, "showType",0));
		cate.setName(WebUtils.getRequestParameterAsString(request, "name"));
		cate.setDesc(WebUtils.getRequestParameterAsString(request, "desc"));
		cate.setOrder(WebUtils.getRequestParameterAsInt(request, "order",0));
		if(cateDao.update(cate)){
			message = "企业文化栏目修改成功!" ;
		}else{
			message = "企业文化栏目修改失败!" ;
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/culture/category-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//企业文化--栏目删除
	@RequestMapping(value="/category-del.do")
	public ModelAndView categoryDel(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId", 0);
		if(cateDao.delete(cateId)){
			message = "企业文化栏目删除成功!" ;
		}else{
			message = "企业文化栏目删除失败!" ;
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/culture/category-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//企业文化--列举所有的栏目
	@RequestMapping(value="/culture-list.do")
	public ModelAndView cultureList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int languageId = (String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE) != null ? Integer.parseInt((String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE)) : 1 ;
		List<CultureCategory> cateList = cateDao.getAll(languageId);
		
		String kw = WebUtils.getRequestParameterAsString(request, "kw");
		kw = StringTools.converISO2UTF8(kw);
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId", 0);
		int isShowIndex = WebUtils.getRequestParameterAsInt(request, "isShowIndex", -1);
		int isShowList = WebUtils.getRequestParameterAsInt(request, "isShowList", -1);
		int isTop = WebUtils.getRequestParameterAsInt(request, "isTop", -1);
		String startTime = WebUtils.getRequestParameterAsString(request, "startTime");
		String endTime = WebUtils.getRequestParameterAsString(request, "endTime");
		
/*		if(StringUtils.isBlank(startTime)){
			startTime = TimeUtils.getDate(new Date(), "yyyy-MM-dd HH:mm:ss", Constants.SEARCH_DAYS);
		}
		if(StringUtils.isBlank(endTime)){
			endTime = TimeUtils.getDate(new Date(),"yyyy-MM-dd HH:mm:ss", 0);
		}*/
		CultureQuery query = new CultureQuery();
		query.setKw(kw);
		query.setLanguageId(languageId);
		query.setCateId(cateId);
		query.setIsShowIndex(isShowIndex);
		query.setIsShowList(isShowList);
		query.setIsTop(isTop);
		query.setStartTime(startTime);
		query.setEndTime(endTime);
		
	    int curPage = WebUtils.getRequestParameterAsInt(request, "curPage", 1);
	    PageBean pageBean = new PageBean();
	    pageBean.setCurPage(curPage);
	    List<CultureInfo> cultureList = cultureDao.getCultureListByCondition(query, pageBean.getStart(), pageBean.getRowsPerPage());
	    pageBean.setMaxRowCount(cultureDao.getCultureCountByCondition(query));
	    pageBean.setMaxPage();
	    pageBean.setPageNoList();
	    
		modelMap.put("cateList", cateList);
		modelMap.put("cultureList", cultureList);
		modelMap.put("query", query);
		modelMap.put("pageBean", pageBean);
		mav.setViewName("super/culture/culture-list");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//企业文化--文化新增、修改页面
	@RequestMapping(value="/culture-show.do")
	public ModelAndView cultureShow(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int cultureId = WebUtils.getRequestParameterAsInt(request, "cultureId", 0);
		int languageId = (String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE) != null ? Integer.parseInt((String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE)) : 1 ;
		List<CultureCategory> cateList = cateDao.getAll(languageId);
		CultureInfo culture = new CultureInfo();
		culture.setLanguageId(languageId);
		String opt = "create" ;
		if(cultureId > 0){
			culture = cultureDao.getCultureById(cultureId);
			String content = culture.getContent();
			culture.setContent(ImageContentUtils.setBasePathToImagePath(request, content));
			opt = "update" ;
		}
		modelMap.put("cateList", cateList);
		modelMap.put("opt", opt);
		modelMap.put("culture", culture);
		modelMap.put("languageId", languageId);
		mav.setViewName("super/culture/culture-show");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//企业文化--文化新增
	@RequestMapping(value="/culture-create.do")
	public ModelAndView cultureCreate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("common/message");
		String message = "" ;
		CultureInfo culture = new CultureInfo();
		String content = WebUtils.getRequestParameterAsString(request, "content") ;
		String textContent = WebUtils.getRequestParameterAsString(request, "textContent");
        int contentType = WebUtils.getRequestParameterAsInt(request, "contentType", 1);
        if ((StringUtils.isBlank(content) && contentType == 1)  || (StringUtils.isBlank(textContent)) && contentType == 0) {
        	modelMap.put("message", "内容不能为空!");
			mav.addAllObjects(modelMap);
			return mav ;
		}
		int languageId = WebUtils.getRequestParameterAsInt(request, "languageId",1);
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId",0);
		
		boolean createFlag = true ;
		//首先判断选定的栏目是否支持列表、详情显示。如果是详情显示的话，需要进行更新操作。
		CultureInfo dbCulture = cultureDao.isCultureExist(languageId,cateId);
		if(dbCulture != null && dbCulture.getCultureCategory().getShowType() == 0){
			createFlag = false ;
			culture = cultureDao.getCultureById(dbCulture.getCultureId());
		}
		
		culture.setLanguageId(languageId);
		culture.setCateId(cateId);
		culture.setTitle(WebUtils.getRequestParameterAsString(request, "title"));
		culture.setAuthor(WebUtils.getRequestParameterAsString(request, "title"));
		culture.setPublishFrom(WebUtils.getRequestParameterAsString(request, "publishFrom"));
		culture.setAuthor(WebUtils.getRequestParameterAsString(request, "author"));
		culture.setPublishDate(new java.util.Date());
		culture.setMetaKeyword(WebUtils.getRequestParameterAsString(request, "metaKeyword"));
		culture.setMetaContent(WebUtils.getRequestParameterAsString(request, "metaContent"));
		culture.setSummary(culture.getMetaContent());
	
		if(contentType == 0 ){
			culture.setContent(textContent);
        }
        else{
        	Map<String,String> map = ImageContentUtils.removeImageBasePathForMap(request, content);
        	culture.setContent(map.get("content"));
        	culture.setFirstImage(map.get("firstImage"));
        }
		culture.setContentType(contentType);
		
		if(createFlag){
			if(cultureDao.create(culture)){
				message = "企业文化创建成功!" ;
			}else{
				message = "企业创建失败!" ;
			}
		}else{
			if(cultureDao.update(culture)){
				message = "企业更新成功!" ;
			}else{
				message = "企业更新失败!" ;
			}
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/culture/culture-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//企业文化--文化修改
	@RequestMapping(value="/culture-update.do")
	public ModelAndView cultureUpdate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		String content = WebUtils.getRequestParameterAsString(request, "content") ;
		if(StringUtils.isBlank(content)){
			modelMap.put("message", "内容不能为空!");
			mav.addAllObjects(modelMap);
			return mav ;
		}
		
		int cultureId = WebUtils.getRequestParameterAsInt(request, "cultureId", 0);
		CultureInfo culture = new CultureInfo();
		culture = cultureDao.getCultureById(cultureId);
		
		culture.setLanguageId(WebUtils.getRequestParameterAsInt(request, "languageId",1));
		culture.setCateId(WebUtils.getRequestParameterAsInt(request, "cateId",0));
		culture.setTitle(WebUtils.getRequestParameterAsString(request, "title"));
		culture.setAuthor(WebUtils.getRequestParameterAsString(request, "title"));
		culture.setPublishFrom(WebUtils.getRequestParameterAsString(request, "publishFrom"));
		culture.setAuthor(WebUtils.getRequestParameterAsString(request, "author"));
        String publishDate = WebUtils.getRequestParameterAsString(request, "publishDate");
        culture.setPublishDate(StringUtils.isBlank(publishDate) ? new java.util.Date() : TimeUtils.formatDate(publishDate));
		culture.setMetaKeyword(WebUtils.getRequestParameterAsString(request, "metaKeyword"));
		culture.setMetaContent(WebUtils.getRequestParameterAsString(request, "metaContent"));
		culture.setSummary(culture.getMetaContent());
		
	
		Map<String,String> map = ImageContentUtils.removeImageBasePathForMap(request, content);
		culture.setContent(map.get("content"));
		culture.setFirstImage(map.get("firstImage"));
		
		if(cultureDao.update(culture)){
			message = "文化修改成功!" ;
		}else{
			message = "文化修改失败!" ;
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/culture/culture-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//企业文化--文化删除
	@RequestMapping(value="/culture-del.do")
	public ModelAndView cultureDel(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "文化删除成功!" ;
		String cultureId = WebUtils.getRequestParameterAsString(request, "cultureId");
		String ids [] = StringUtils.split(cultureId, ",") ;
		boolean flag = false ;
		for(String id : ids){
			flag = cultureDao.delete(Integer.parseInt(id));
			if(flag == false){
				message = "文化删除失败!" ;
				break ;
			}
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/culture/culture-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//企业文化--首页显示设置
	@RequestMapping(value="/culture-setIndex.do")
	public ModelAndView cultureSetIndex(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		String cultureId = WebUtils.getRequestParameterAsString(request, "cultureId");
		String flag = WebUtils.getRequestParameterAsString(request, "flag");
		if(StringUtils.isNotBlank(flag) && flag.matches("\\d+")){
			String nds [] = StringUtils.split(cultureId, ",") ;
			int cultureIds [] = new int[nds.length] ;
			int i = 0 ;
			for(String nd : nds){
				cultureIds[i] = Integer.valueOf(nd);
				i ++ ;
			}
			if(cultureDao.setShowIndex(Integer.valueOf(flag),cultureIds)){
				message = "文化" + (flag.equals("1") ? "设置首页显示" : "取消首页显示") + "成功!" ;
			}else{
				message = "文化" + (flag.equals("1") ? "设置首页显示" : "取消首页显示") + "失败!" ;
			}
		}
		
		modelMap.put("message", message);
		modelMap.put("href", "super/culture/culture-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//企业文化--列表页显示设置
	@RequestMapping(value="/culture-setList.do")
	public ModelAndView cultureSetList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		String cultureId = WebUtils.getRequestParameterAsString(request, "cultureId");
		String flag = WebUtils.getRequestParameterAsString(request, "flag");
		if(StringUtils.isNotBlank(flag) && flag.matches("\\d+")){
			String nds [] = StringUtils.split(cultureId, ",") ;
			int cultureIds [] = new int[nds.length] ;
			int i = 0 ;
			for(String nd : nds){
				cultureIds[i] = Integer.valueOf(nd);
				i ++ ;
			}
			if(cultureDao.setShowList(Integer.valueOf(flag),cultureIds)){
				message = "文化" + (flag.equals("1") ? "设置列表页显示" : "取消列表页显示") + "成功!" ;
			}else{
				message = "文化" + (flag.equals("1") ? "设置列表页显示" : "取消列表页显示") + "失败!" ;
			}
		}
		
		modelMap.put("message", message);
		modelMap.put("href", "super/culture/culture-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
}